<?php
require_once('./class/Adhesion.php');
require_once('./class/Adherent.php');
require_once('./class/Association.php');
switch (@$_GET['action']){
    default:
        echo _ADHESION_;
    break;

    case "list":
        $id_association = (isset($_GET['id_association'])) ? $_GET['id_association'] : null;
        if(isset($_POST['start_date']) || isset($_POST['end_date'])){
            if(!isset($_GET['notPayed'])){
                $adhesion_array = Adhesion::getAllAdhesionWithDate($id_association, $_POST['start_date'], $_POST['end_date']);
            }
            else{
                $adhesion_array = Adhesion::getAllAdhesionWithDateNotPayed($id_association, $_POST['start_date'], $_POST['end_date']);
            }
        }
        else{
            if(!isset($_GET['notPayed'])){
                $adhesion_array = Adhesion::getAllAdhesion($id_association);
            }
            else{
                $adhesion_array = Adhesion::getAllAdhesionNotPayed($id_association);
            }
        }
        $linkid = (isset($_GET['id_association'])) ? '&id_association='.$_GET['id_association'] : '';
        $link = (isset($_GET['notPayed'])) ? '<a href="?module=adhesion&action=list'.$linkid.'">'._VIEW_ALL_ADHESION_.'</a>' : '<a href="?module=adhesion&action=list&notPayed=1'.$linkid.'">'._VIEW_NOT_PAYED_ADHESION_.'</a>';
        $actionForm = (isset($_GET['notPayed'])) ? '&notPayed=1' : '';
        $html = $link.'
        		<form action="?module=adhesion&action=list'.$linkid.$actionForm.'" method="post">
        			<table>
            			<tr>
            				<td>'._FILTER_BY_DATE_.'</td>
            				<td>'._START_DATE_.' : <input type="text" name="start_date" value="'.@$_POST['start_date'].'" class="datepicker" /></td>
            				<td>'._END_DATE_.' : <input type="text" name="end_date" value="'.@$_POST['end_date'].'" class="datepicker" /></td>
            				<td><input type="submit" value="'._CONTINUE_.'" /></td>
            			</tr>	
        		</form>
        		<table class="tablesorter">
							<thead>
								<th>'._ID_INDIVIDU_.'</th>
								<th>'._NOM_INDIVIDU_.'</th>
								<th>'._PRENOM_INDIVIDU_.'</th>
								<th>'._EMAIL_ADHERENT_.'</th>
								<th>'._FILIERE_ADHERENT_.'</th>
								<th>'._PROMOTION_ADHERENT_.'</th>
								<th>'._ID_ASSOCIATION_.'</th>
								<th>'._NOM_ASSOCIATION_.'</th>
								<th>'._LOGIN_.'</th>
								<th>'._COUT_COTISATION_.'</th>
								<th>'._DATE_INSCRIPTION_.'</th>
								<th>'._EDIT_.'</th>
								<th>'._DELETE_.'</th>
							</thead><tbody>';
        $i = 0;
        foreach($adhesion_array as $a){
            $association = Association::getAssociation($a->getIdAssociation());
            $adherent = Adherent::getAdherent($a->getIdIndividu());
            $odd = ($i % 2 == 0) ? 'class="odd"' : "";
            $i++;
            $html .= '<tr '.$odd.'>
								<td>'.$a->getIdIndividu().'</td>
								<td>'.$adherent->getNom().'</td>
								<td>'.$adherent->getPrenom().'</td>
								<td>'.$adherent->getEmail().'</td>
								<td>'.$adherent->getFiliere().'</td>
								<td>'.$adherent->getPromotion().'</td>
								<td>'.$a->getIdAssociation().'</td>
								<td>'.$association->getNom().'</td>
								<td>'.$a->getLogin().'</td>
								<td>'.$a->getCoutCotisation().'</td>
								<td>'.$a->getDateInscription().'</td>
							<td><a href="?module=adhesion&action=modifyForm&id_individu='.$a->getIdIndividu().'&id_association='.$a->getIdAssociation().'">'._EDIT_.'</a></td>
							<td><a href="?module=adhesion&action=delete&id_individu='.$a->getIdIndividu().'&id_association='.$a->getIdAssociation().'">'._DELETE_.'</a></td>
						</tr>';
        }
        $html .= '</tbody></table>';
        echo $html;
        break;

    case "addForm":
        echo Adhesion::getAddFormIndividu();
        break;

    case "addContinue":
        $adherent = Adherent::getAdherent($_POST['id_individu']);
        if($adherent == false){
            echo Adhesion::getAddFormNonAdherent($_POST['id_individu']);
        }
        else{
            echo Adhesion::getAddFormAdherent($_POST['id_individu']);
        }
        break;

    case "modifyForm":
        $adhesion = Adhesion::getAdhesion($_GET['id_individu'], $_GET['id_association']);
        echo $adhesion->getModifyForm();
        break;
         
    case "doModify":
        $a = new Adhesion();
        $a->setIdIndividu($_POST['id_individu']);
        $a->setIdAssociation($_POST['id_association']);
        $a->setLogin($_POST['login']);
        $a->setPassword($_POST['password']);
        $a->setDateInscription($_POST['date_inscription']);
        $a->setCoutCotisation($_POST['cout_cotisation']);
        $a->addOrModify();
        echo _ADHESION_.' '._MODIFIED_;
        break;

    case "doAddNonAdherent":
        $adherent = new Adherent();
        $adherent->setId($_POST['id_individu']);
        $adherent->setEmail($_POST['email']);
        $adherent->setFiliere($_POST['filiere']);
        $adherent->setPromotion($_POST['promotion']);
        $adherent->addOrModify();

        $adhesion = new Adhesion();
        $adhesion->setIdIndividu($_POST['id_individu']);
        $adhesion->setLogin($_POST['login']);
        $adhesion->setPassword($_POST['password']);
        $adhesion->setDateInscription($_POST['date_inscription']);
        $adhesion->setCoutCotisation($_POST['cout_cotisation']);
        $adhesion->setIdAssociation($_POST['id_association']);
        $adhesion->addOrModify();
        echo _ADHESION_.' '._ADDED_;
        break;

    case "doAddAdherent":
        $adhesion = new Adhesion();
        $adhesion->setIdIndividu($_POST['id_individu']);
        $adhesion->setLogin($_POST['login']);
        $adhesion->setPassword($_POST['password']);
        $adhesion->setDateInscription($_POST['date_inscription']);
        $adhesion->setCoutCotisation($_POST['cout_cotisation']);
        $adhesion->setIdAssociation($_POST['id_association']);
        $adhesion->addOrModify();
        echo _ADHESION_.' '._ADDED_;
        break;

    case "delete":
        $a = new Adhesion();
        $a->setIdAssociation($_GET['id_association']);
        $a->setIdIndividu($_GET['id_individu']);
        $a->delete();
        echo _ADHESION_.' '._DELETED_;
        break;
}
?>
